﻿//ОБНОВИТЬ
function RefreshDayDocumentsGrid() {
    var dayDocs = Ext.getCmp("administration.grid.dayDocuments_ID");
    dayDocs.store.load();
};

//ОТКРЫТИЕ ОКНА РЕДАКТИРОВАНИЯ ДОКУМЕНТА
function openDocumentEditor(doc) {
        var strUrl = 'http://' + document.location.host + doc.url;
        var name=doc.FileName + "(" + doc.tags+")";
        window.open(strUrl);
}

///////////////////////////////////////////////////////Список дневных документов ///////////////////////////////////////////////////////
Ext.define('administration.grid.dayDocuments', {
    extend: 'Ext.grid.GridPanel',
    width: "100%",
    id: 'administration.grid.dayDocuments_ID',
    collapsible: false,
    tbar: [
          { xtype: 'textfield', id: 'txtDragName', width: 397 },
          { xtype: 'checkboxfield', id: 'chStage1', boxLabel: 'Считанны данные', handler: RefreshDayDocumentsGrid },
          { xtype: 'checkboxfield', id: 'chStage2', boxLabel: 'Обработанны МНН' },
          { xtype: 'tbfill' },
          { xtype: 'button', text: 'Найти', handler: RefreshDayDocumentsGrid }
        ],
    region: 'center',
    margins: '5 0 0 0',
    autoExpandColumn: 'FileInfo',
    store: new store.DayDocument(),
    columns: [
                { dataIndex: 'DocID', width: 70, renderer: grid_DocumentGrid_RowPointer },
                { dataIndex: 'DocID', width: 70, header: 'DocID' },
                { dataIndex: 'Notification.NotificationNumber', width: 170, header: 'NotificationNumber' },
                { dataIndex: 'TagInfo', type: 'int', renderer: grid_DocumentGrid_tagInfoRender, width: 670, header: 'FileInfo' },
                { dataIndex: 'OneStepIsOK', width: 50, header: 'Stage 1', renderer: grid_DocumentGrid_StepOneRender },
                { dataIndex: 'SecondStepIsOK', width: 50, header: 'Stage 2' }
             ],
    initComponent: function () {
        Ext.apply(this, {});
        this.bbar = new Ext.PagingToolbar({
            store: this.store,
            displayInfo: true,
            displayMsg: 'Показаны документы {0} - {1} of {2}',
            emptyMsg: "Документов не найдено"
        });

        //считываение параметров загрузки
        this.imLoad = function (grid, operation) {
            this.loadParams["DragName"] = Ext.getCmp('txtDragName').getValue();
            this.loadParams["OnlyStage1"] = Ext.getCmp('chStage1').getValue();
            operation.params = { args: Ext.encode(this.loadParams) };

        };

        this.store.on("beforeload", this.imLoad);

        //объект параметры загрузки
        this.store.loadParams = { Day: null };

        this.store.pageSize = Global_visualConfig.dayDocumentsPageSize();

        this.listeners = {
            'cellclick':
                        { fn: function (grid, rowIndex, columnIndex, e) {
                            //редактирование документа
                            if (columnIndex < 4) {
                                //Ext.getCmp("administration.grid.dayDocuments_ID").disable();
                                var feture = new Feature("AdministrationPort.ashx");
                                result = feture.Execute({ Procedure: 'OpenHtmlDocument', args: { DocID: e.data.DocID, Part: e.data.Part, PublicFileName: e.data.PublicFileName, iTransportDay: e.data.iTransportDay, FullID: e.data.GetFullID, Tags: e.data.TagInfo.Tags} });
                                //Ext.getCmp("administration.grid.dayDocuments_ID").enable();
                                var doc = { url: result.url, FileName: e.data.FileName, tags: e.data.TagInfo.TagsAsStr };
                                openDocumentEditor(doc);
                            }
                            //редактирование статуса 1 этапа
                            if (columnIndex == 4) {

                                var rec = e.store.getById(e.data.GetFullID);
                                var v = rec.data["FirstStepIsOK"];
                                rec.set("FirstStepIsOK", !v);

                                e.store.save();
                            }

                            if (columnIndex == 5) {
                                alert('открыть редатор');
                            }

                        }
                        }
        };

        administration.grid.dayDocuments.superclass.initComponent.apply(this, arguments);
    }
});

//RENDER
function grid_DocumentGrid_tagInfoRender(p, r, l) {
    return "<div>" + l.data.FileName + "</div>" + "<div>" + l.data.TagInfo.TagsAsStr + "</div>"
}

function grid_DocumentGrid_StepOneRender(p, r, l) {
    if (l.data.FirstStepIsOK)
            return "<img alt='' src='images/galka.gif' />";   
    return "";
}

function grid_DocumentGrid_RowPointer(p, r, l) {
    return "<img alt='' src='images/arrow.gif' />";    
}







